package cg.studio.practise;


import java.io.*;
import java.util.*;

/*
Problem: You are given a character array like "d3b1c1d1e4f0g11".
You need to expand the array by repeating the characters preceding each of the numbers. For example, the above character array expands to "dddbcdeeeeggggggggggg".
The passed in character array will have more than enough trailing spaces such that you can modify the array in place.
*/

/*
 * To execute Java, please define "static void main" on a class
 * named Solution.
 *
 * If you need more classes, simply define them inline.
 */
class StringExpand {
  public static void main(String[] args) {
    ArrayList<String> strings = new ArrayList<String>();
    strings.add("Hello, World!");
    strings.add("Welcome to CoderPad.");
    strings.add("This pad is running Java 8.");

    for (String string : strings) {
      System.out.println(string);
    }
    
    char[] array = "d3b1c1d1e4f0g11                         ".toCharArray();
    expand(array);
    for (char ch : array)
      System.out.print(ch);
  }
  
  private static void expand(char[] array) {
    int i = 0;
    for( ; ; ++i )
    {
      char c = array[i];
      if( c==' ' )
        break;
    }
    --i;
    System.out.println( "i=" + i );
    System.out.println( "array[i]=" + array[i] );
    
    int right = array.length-1;
    int expandedLen = 0;
    while( i >= 0 )
    {
      int num = 0;
      int numLen = 1;
      while( array[i] >='0' && array[i] <='9' )
      {
        num += ( numLen * (array[i] -'0')  );
        numLen *= 10;
        --i;
      }
      expandedLen += num;
      System.out.println( "num=" + num );
      System.out.println( "i=" + i );
      System.out.println( "array[i]=" + array[i] );
      for( int j=0; j<num; ++j, right-- )
      {
        array[right] = array[i];
      }
      --i;
    }
    
    int start = array.length - expandedLen;
    int k=0;
    for( ; k<expandedLen; ++k, ++start )
    {
      array[k] = array[start];
    }
    for( ; k<array.length; ++k )
    {
      array[k] = ' ';
    }
    
  }
  
 
  
}
